package com.ktjiaotu.action;

import org.apache.log4j.Logger;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Arrays;

/**
 * @Author: DABINGCHNEGJIAO
 * @CreateTime: 2025-06-05
 * @Description: jdk动态代理工厂
 * @Version: 1.0
 */
public class JdkDynamicProxyFatctory {
    private static final Logger logger = Logger.getLogger(JdkDynamicProxyFatctory.class);

    public static <T> T create(T target) {
        InvocationHandler handler = (Object proxy, Method method, Object[] args) -> {
            logger.debug(Arrays.toString(args) + "编写准备->打开了电脑");
            logger.debug(Arrays.toString(args) + "编写准备->打开了软件");
            Object result = method.invoke(target, args);
            logger.debug(Arrays.toString(args) + "编写完成->关闭了软件");
            logger.debug(Arrays.toString(args) + "编写完成->关闭了电脑");
            return result;
        };
        return (T) Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(), handler);
    }
}
